package com.example.datasourcedemo.factory;


import com.zaxxer.hikari.HikariDataSource;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

@Component
public class DataSourceFactory {
    public static DataSource createDataSource(String ip, int port, String username, String password, String type) {
        HikariDataSource dataSource = new HikariDataSource();
        String url = "";
        if ("mysql".equalsIgnoreCase(type)) {
            dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
            url = String.format("jdbc:mysql://%s:%d/", ip, port);
        } else if ("dm".equalsIgnoreCase(type)) {
            dataSource.setDriverClassName("dm.jdbc.driver.DmDriver");
            url = String.format("jdbc:dm://%s:%d", ip, port);
        } else {
            throw new IllegalArgumentException("Unsupported data source type");
        }
        dataSource.setJdbcUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setMaximumPoolSize(5); // 按需调整
        dataSource.setMinimumIdle(1);
        return dataSource;
    }
}
